/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import entities.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.aptech.session.beans.*;

/**
 *
 * @author Trong Nguyen
 */
@ManagedBean
@RequestScoped
public class UtilsControl implements Serializable {

    @EJB
    private EventFacade eventFacade;
    @EJB
    private PrizeFacade prizeFacade;
    @EJB
    private WinnerFacade winnerFacade;
    @EJB
    private RegistrationFacade registrationFacade;
    @EJB
    private UserFacade userFacade;

    public List<User> birthdayList() {
        List<User> users = userFacade.getAllUserWithBirhday();
        if (null != users) {
            System.out.println(" --------------------birthdays: " + users.size());
        }
        return users;
    }

    /**
     * get all prize within 30 days ago.
     *
     * @return List<SumaryPrize>
     */
    public List<SumaryPrize> selectAllPrizeWithinAMonth() {
        ArrayList<SumaryPrize> sumaryPrizesList = new ArrayList<SumaryPrize>();
        SumaryPrize temp = null;
        // get list<prize> within a month.
        List<Prize> prizeList = prizeFacade.getPrizeListWinthinAmonth();
        try {
            // get Registration from prize
            for (Prize prize : prizeList) {

                List<Winner> winnerList = prize.getWinnerList();
                for (Winner winner : winnerList) {
                    Registration registration = winner.getRegId();
                    // find User by registration
                    User user = registration.getUserName();
                    // find event by prize
                    Event event = prize.getEventId();
                    // create new WinnerDetails
                    WinnerDetails winnerDetails = new WinnerDetails(user, prize);
                    //create new SumaryPrize
                    SumaryPrize sumaryPrize = new SumaryPrize(event);
                    // add WinnerDetails into sumaryPrizesList list
                    sumaryPrize.getWinnerList().add(winnerDetails);
                    // if sumaryPrizesList  contains sumaryPrize
                    if (sumaryPrizesList.contains(sumaryPrize)) {
                        int pos = sumaryPrizesList.indexOf(sumaryPrize);
                        temp = sumaryPrizesList.get(pos);
                        temp.getWinnerList().add(winnerDetails);
                    } else {
//                if sumaryPrizesList does not contain sumaryPrize
                        sumaryPrizesList.add(sumaryPrize);
                    }
                }


            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sumaryPrizesList;
    }

    /**
     * Get all prizes by an event
     *
     * @param event
     * @return
     */
    public List<SumaryPrize> selectAllPrizeByEvent(Event event) {
        ArrayList<SumaryPrize> sumaryPrizesList = new ArrayList<SumaryPrize>();

        if (null == event) {
            System.out.println("---selectAllPrizeByEvent: event is NULL-----");
            return sumaryPrizesList;
        }
        SumaryPrize sumaryPrize = new SumaryPrize(event);
        // get list<prize> within a month.
        List<Prize> prizeList = event.getPrizeList();
        try {
            // get Registration from prize
            for (Prize prize : prizeList) {

                List<Winner> winnerList = prize.getWinnerList();
                for (Winner winner : winnerList) {
                    Registration registration = winner.getRegId();
                    // find User by registration
                    User user = registration.getUserName();
                    // create new WinnerDetails
                    WinnerDetails winnerDetails = new WinnerDetails(user, prize);
                    //create new SumaryPrize

                    // add WinnerDetails into sumaryPrizesList list
                    sumaryPrize.getWinnerList().add(winnerDetails);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sumaryPrizesList;
    }
}
